package com.compus_help.config;


import com.compus_help.common.JacksonObjectMapper;
import com.compus_help.interceptor.JwtInterceptor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;


import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@EnableWebMvc
@Configuration

public class WebConfig implements WebMvcConfigurer {


    @Autowired
    private JwtInterceptor jwtInterceptor;
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 添加静态资源处理器，处理包括 .dat 文件在内的静态资源
        registry.addResourceHandler("/officescan/**")
                .addResourceLocations("classpath:/static/officescan/", "classpath:/public/officescan/")
                .setCachePeriod(3600)
                .resourceChain(true);

        // 通用静态资源处理器
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/", "classpath:/public/");
    }
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 移除默认的Jackson转换器
        converters.removeIf(converter -> converter instanceof MappingJackson2HttpMessageConverter);

        // 添加自定义的Jackson转换器
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(new JacksonObjectMapper());
        converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));

        // 将自定义转换器添加到列表开头，确保优先级最高
        converters.add(0, converter);
    }
}
